package com.agent.vpsserver.common.aop;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * description
 *
 * @author <a href="mailto:ludezh@dingtalk.com"> ludezh </a>
 * @version 1.0.0   2018/12/27,10:14
 * @since 1.0.0     2018/12/27,10:14
 */
@Slf4j
@Aspect
@Component
public class ControllerAOP {

    /**
     * controller层切点
     */
    @Pointcut("execution(* com.agent.vpsserver.modules.*.controller.*.*(..))")
    public void controllerPointcut() {
    }

    /**
     * controller层切点
     */
    @Pointcut("execution(* com.agent.vpsserver.modules.*.controller.*.*.*(..))")
    public void controllerPointcut1() {
    }

    @Around("controllerPointcut() || controllerPointcut1()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        String className = null;
        String methodName = null;
        Object[] args = null;
//        try {
        className = pjp.getTarget().getClass().getName();
        methodName = pjp.getSignature().getName();
        args = pjp.getArgs();
        log.info("################# 请求体类名：{},方法名：{},参数：{}", className, methodName, ArrayUtils.toString(args));
        Object proceed = pjp.proceed();
        log.info("################# 请求结果返回值：{}", JSON.toJSONString(proceed));
        return proceed;
//        } catch (Throwable e) {
//            // BusinessException自定义异常类不打印日志
//            if (e instanceof BusinessException) {
//                throw e;
//            }
//            // 在事务@Transaction下,抛出异常时已经回滚了,此时捕获的异常不属于事务内
//            log.error("######### 类名：" + className + ",方法名：" + methodName + ",参数：" + ArrayUtils.toString(args) + ",异常信息：", e);
//            throw e;
//        }
    }
}
